package com.rose.springbootgeneratefile.api;

import com.rose.springbootgeneratefile.config.Context;
import com.rose.springbootgeneratefile.utils.StringUtility;

import static com.rose.springbootgeneratefile.internal.utils.JavaBeansUtil.getCamelCaseString;

public class FullyQualifiedTable {
    /** The introspected catalog. */
    private String introspectedCatalog;

    /** The introspected schema. */
    private String introspectedSchema;

    /** The introspected table name. */
    private String introspectedTableName;

    /** The runtime catalog. */
    private String runtimeCatalog;

    /** The runtime schema. */
    private String runtimeSchema;

    /** The runtime table name. */
    private String runtimeTableName;

    /** The domain object name. */
    private String domainObjectName;

    /** The domain object sub package. */
    private String domainObjectSubPackage;

    /** The alias. */
    private String alias;

    /** The ignore qualifiers at runtime. */
    private boolean ignoreQualifiersAtRuntime;

    /** The beginning delimiter. */
    private String beginningDelimiter;

    /** The ending delimiter. */
    private String endingDelimiter;

    public FullyQualifiedTable(String introspectedCatalog,
                               String introspectedSchema, String introspectedTableName,
                               String domainObjectName, String alias,
                               boolean ignoreQualifiersAtRuntime, String runtimeCatalog,
                               String runtimeSchema, String runtimeTableName,
                               boolean delimitIdentifiers, Context context) {
        super();
        this.introspectedCatalog = introspectedCatalog;
        this.introspectedSchema = introspectedSchema;
        this.introspectedTableName = introspectedTableName;
        this.ignoreQualifiersAtRuntime = ignoreQualifiersAtRuntime;
        this.runtimeCatalog = runtimeCatalog;
        this.runtimeSchema = runtimeSchema;
        this.runtimeTableName = runtimeTableName;

        if (StringUtility.stringHasValue(domainObjectName)) {
            int index = domainObjectName.lastIndexOf('.');
            if (index == -1) {
                this.domainObjectName = domainObjectName;
            } else {
                this.domainObjectName = domainObjectName.substring(index + 1);
                this.domainObjectSubPackage = domainObjectName.substring(0, index);
            }
        }

        if (alias == null) {
            this.alias = null;
        } else {
            this.alias = alias.trim();
        }

//        beginningDelimiter = delimitIdentifiers ? context
//                .getBeginningDelimiter() : ""; //$NON-NLS-1$
//        endingDelimiter = delimitIdentifiers ? context.getEndingDelimiter()
//                : ""; //$NON-NLS-1$
    }

    public String getSubPackageForModel(boolean isSubPackagesEnabled) {
        StringBuilder sb = new StringBuilder();
        sb.append(getSubPackageForClientOrSqlMap(isSubPackagesEnabled));

        if (StringUtility.stringHasValue(domainObjectSubPackage)) {
            sb.append('.');
            sb.append(domainObjectSubPackage);
        }

        return sb.toString();
    }
    public String getSubPackageForClientOrSqlMap(boolean isSubPackagesEnabled) {
        StringBuilder sb = new StringBuilder();
        if (!ignoreQualifiersAtRuntime && isSubPackagesEnabled) {
            if (StringUtility.stringHasValue(runtimeCatalog)) {
                sb.append('.');
                sb.append(runtimeCatalog.toLowerCase());
            } else if (StringUtility.stringHasValue(introspectedCatalog)) {
                sb.append('.');
                sb.append(introspectedCatalog.toLowerCase());
            }

            if (StringUtility.stringHasValue(runtimeSchema)) {
                sb.append('.');
                sb.append(runtimeSchema.toLowerCase());
            } else if (StringUtility.stringHasValue(introspectedSchema)) {
                sb.append('.');
                sb.append(introspectedSchema.toLowerCase());
            }
        }

        // TODO - strip characters that are not valid in package names
        return sb.toString();
    }

    public String getDomainObjectName() {
        if (StringUtility.stringHasValue(domainObjectName)) {
            return domainObjectName;
        } else if (StringUtility.stringHasValue(runtimeTableName)) {
            return getCamelCaseString(runtimeTableName, true);
        } else {
            return getCamelCaseString(introspectedTableName, true);
        }
    }
}
